import io.jsonwebtoken.*;
import org.aspectj.weaver.ast.Var;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Token
 * @Description
 * @Author Mr.L
 * @Date 21:15 2019/11/20
 * @Version 2.1
 **/
public class Token {
    @Test
    public void createToken(){
        //创建JWT
        JwtBuilder builder = Jwts.builder();
        //构建头信息
        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("keyId","JWT");
        builder.setHeader(map);
        //构建载荷信息
        builder.setId("001");
        builder.setIssuer("张三");
        builder.setIssuedAt(new Date());
        //添加签名
        builder.signWith(SignatureAlgorithm.HS256, "itheima");
        builder.setExpiration(new Date(System.currentTimeMillis() + 30000));

        // 生成token
        String token = builder.compact();
        System.out.println("token:" + token);

    }

    @Test
    public void testParseToken(){
        //被解析的令牌
        String token = "eyJrZXlJZCI6IkpXVCIsImFsZyI6IkhTMjU2In0.eyJqdGkiOiIwMDEiLCJpc3MiOiLlvKDkuIkiLCJpYXQiOjE1NzQyNTY3OTgsImV4cCI6MTU3NDI1NjgyOX0.3kTv6_47rdqTNV3a35Rmo5uTm8-a1tiweYFuqJ8jDF8";
        //创建解析对象
        JwtParser parser = Jwts.parser();
        parser.setSigningKey("itheima");
        Claims claims = parser.parseClaimsJws(token).getBody();
        System.out.println("claims = " + claims);
    }
}
